package com.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.task.bean.Student;
import com.task.bean.TeaAssStu;
import com.task.bean.Teacher;
import com.task.mapper.StudentMapper;
import com.task.mapper.TeaAssStuMapper;
import com.task.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liuzhe
 * @since 2021-04-10
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeaAssStuMapper teaAssStuMapper;

    @Override
    public List<Student> queryStuByTeaId(int teaId) {
        return studentMapper.queryStuByTeaId(teaId);
    }

    @Override
    public Student queryStuByUsernameAndPassword(Student student) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("username", student.getUsername());
        queryWrapper.eq("password", student.getPassword());
        List<Student> list = baseMapper.selectList(queryWrapper);
        return list.get(0);
    }

    @Override
    public List<Teacher> queryTeaByStuId(int stuId) {
        return studentMapper.queryTeaByStuId(stuId);
    }

    @Override
    public List<Student> queryStuPage(int pageNum, int pageSize) {
        IPage<Student> studentPage = new Page<>(pageNum, pageSize);
        IPage page = studentMapper.selectPage(studentPage, null);
        return page.getRecords();
    }

    @Override
    public List<Student> queryStuNo(int teaId) {
        // 查询出学生ids
//        List<TeaAssStu> teaAssStus = teaAssStuMapper.selectList(new QueryWrapper<TeaAssStu>().eq("tea_id", teaId));
//        List<Integer> teaAssStuIn = teaAssStus.stream().map(item -> item.getStuId()).collect(Collectors.toList());
//        return studentMapper.queryStuNo(teaAssStuIn);
        // 全部学生
        List<Integer> studentList = studentMapper.selectList(null).stream().map(item -> item.getId()).collect(Collectors.toList());
        List<TeaAssStu> teaAssStus = teaAssStuMapper.selectList(new QueryWrapper<TeaAssStu>().eq("tea_id", teaId));
        List<Integer> teaAssStuIn = teaAssStus.stream().map(item -> item.getStuId()).collect(Collectors.toList());
        studentList.removeAll(teaAssStuIn);
        return studentMapper.selectBatchIds(studentList);
    }
}
